package cn.ld.fj.web.statistic;

import cn.ld.fj.pojo.PeopleStatisticPojo;
import cn.ld.fj.pojo.SaleStatisticPojo;
import cn.ld.fj.pojo.Top5FoodStatisticPojo;
import cn.ld.fj.service.statistic.StatisticManager;
import cn.ld.fj.web.JsonActionSupport;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.opensymphony.xwork2.ActionSupport;
import net.esoar.modules.utils.encode.JsonBinder;
import net.esoar.modules.utils.web.struts2.Struts2Utils;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;


/**
 * @author lixiangjun
 */
@Namespace("/statistic")
@Results({@Result(name = JsonActionSupport.RELOAD, location = "statistic.action", type = "redirect")})
public class StatisticAction extends ActionSupport {

    private final Logger logger = LoggerFactory.getLogger(StatisticAction.class);

    @Autowired
    private StatisticManager statisticManager;


    public String xse() throws Exception {


        return "xse";
    }


    public void xseData() {


        Map<String, Object> condtion = Maps.newHashMap();
        List<SaleStatisticPojo> saleStatisticPojoList = statisticManager.saleStatistics(condtion);
        try {
            Struts2Utils.getResponse().getWriter().print(JsonBinder.buildNonDefaultBinder().toJson(saleStatisticPojoList));
            Struts2Utils.getResponse().getWriter().flush();
        } catch (IOException e) {
            e.fillInStackTrace();
        }

    }


    public String top5food() throws Exception {
        return "top5food";
    }


    public void top5foodData() {


        Map<String, Object> condtion = Maps.newHashMap();
        List<Top5FoodStatisticPojo> top5FoodStatisticPojos = statisticManager.top5FoodStatistic(condtion);
        try {
            Struts2Utils.getResponse().getWriter().print(JsonBinder.buildNonDefaultBinder().toJson(top5FoodStatisticPojos));
            Struts2Utils.getResponse().getWriter().flush();
        } catch (IOException e) {
            e.fillInStackTrace();
        }

    }


    public String people() throws Exception {

        return "people";
    }


    public void peopleData() throws Exception {
        Map<String, Object> condtion = Maps.newHashMap();
        List<PeopleStatisticPojo> peopleStatisticPojos = statisticManager.peopleStatistic(condtion);
        try {
            Struts2Utils.getResponse().getWriter().print(JsonBinder.buildNonDefaultBinder().toJson(peopleStatisticPojos));
            Struts2Utils.getResponse().getWriter().flush();
        } catch (IOException e) {
            e.fillInStackTrace();
        }
    }


}
